﻿using CarRental.DDL;
using CarRental.Helpers;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View.Order {
    public partial class EditOrder : System.Web.UI.Page {
        DbAccess __dbAccess = new DbAccess();
        order_list __orderLst = new order_list();
        order_detail __orderDtl = new order_detail();
        string __orderId = "";

        protected void Page_Load(object sender, EventArgs e) {
            if (Shared.LoginInfo.UserType == "CUSTOMER") {
                ddlStatus.Enabled = false;
                ddlDamage.Enabled = false;
                tbRealReturn.Enabled = false;
                tbAdditionalFee.Enabled = false;
            }

            if (!IsPostBack) {
                __orderId = Request.QueryString["OrderId"];

                ddlDamage.DataSource = new CarRental.Helpers.OptionItems.YesNoLst(false);
                ddlDamage.DataValueField = "Key";
                ddlDamage.DataTextField = "Title";
                ddlDamage.DataBind();

                ddlStatus.DataSource = new CarRental.Helpers.OptionItems.OrderStatusLst(false);
                ddlStatus.DataValueField = "Key";
                ddlStatus.DataTextField = "Title";
                ddlStatus.DataBind();

                __orderLst = __dbAccess.GetOrderLst().Where(w => w.order_no == __orderId).FirstOrDefault();
                __orderDtl = __dbAccess.GetOrderDtl().Where(w => w.order_no == __orderId).FirstOrDefault();
                if (__orderLst != null && __orderDtl != null) {
                    InitialRecord();
                }
            }

        }

        private void InitialRecord() {
            double _Days = (__orderDtl.prefer_return_date - __orderDtl.start_rent_date).TotalDays;
            int _totaldays = (int)Math.Ceiling(_Days) + 1;

            tbOrderNo.Text = __orderLst.order_no;
            tbCarNo.Text = __orderDtl.car_no;
            tbRentFDate.Text = __orderDtl.start_rent_date.ToString("yyyy-MM-dd");
            tbRentTDate.Text = __orderDtl.prefer_return_date.ToString("yyyy-MM-dd");
            tbRealReturn.Text = (__orderDtl.real_returm_date == null || __orderDtl.real_returm_date == DateTime.ParseExact("0001-01-01", "yyyy-MM-dd", null)) ? "" : __orderDtl.real_returm_date.ToString("yyyy-MM-dd");
            tbTotalDays.Text = _totaldays.ToString();
            tbAmount.Text = __orderLst.order_price.ToString();
            tbAdditionalFee.Text = __orderDtl.additional_fee.ToString();
            ddlStatus.SelectedValue = __orderLst.status;
            ddlDamage.SelectedValue = __orderDtl.damage;
            tbRemarks.Text = __orderDtl.remarks;

        }

        protected void btnEditOrder_Click(object sender, EventArgs e) {
            var _orderLst = __dbAccess.GetOrderLst().Where(w => w.order_no == tbOrderNo.Text).FirstOrDefault();
            if (Shared.LoginInfo.UserType == "CUSTOMER") {
                _orderLst.customer_id = Shared.LoginInfo.LoginName;
            } else {
                _orderLst.staff_id = Shared.LoginInfo.LoginName;
            }
            _orderLst.status = ddlStatus.SelectedValue;
            _orderLst.last_updated_by = Shared.LoginInfo.LoginName;
            _orderLst.last_update_date_time = DateTime.Now;

            __dbAccess.UpdateOrderLst(_orderLst);

            var _orderLDtl = __dbAccess.GetOrderDtl().Where(w => w.order_no == tbOrderNo.Text).FirstOrDefault();
            _orderLDtl.remarks = tbRemarks.Text;
            _orderLDtl.damage = ddlDamage.SelectedValue;
            _orderLDtl.start_rent_date = DateTime.ParseExact(tbRentFDate.Text, "yyyy-MM-dd", null);
            _orderLDtl.prefer_return_date = DateTime.ParseExact(tbRentTDate.Text, "yyyy-MM-dd", null);
            if (!string.IsNullOrEmpty(tbRealReturn.Text)) {
                _orderLDtl.prefer_return_date = DateTime.ParseExact(tbRealReturn.Text, "yyyy-MM-dd", null);
            }
            _orderLDtl.additional_fee = decimal.Parse(string.IsNullOrEmpty(tbAdditionalFee.Text) ? "" : tbAdditionalFee.Text);

            __dbAccess.UpdateOrderDtl(_orderLDtl);
            MessageBox.Show("Update order success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Response.Redirect(ResolveUrl("~/View/Order/ReviewOrder.aspx"));
        }

        protected void btnBack_Click(object sender, EventArgs e) {
            Response.Redirect(ResolveUrl("~/View/Order/ReviewOrder.aspx"));
        }
    }
}